<?
//Image library 0.0.4
class image{
    private $width;     //New width
    private $height;    //New height
    private $x1;        //Left-top coord x
    private $y1;        //Left-top coord y
    private $x2;        //Right-bottom coord x
    private $y2;        //Right-bottom coord y
    private $name;      //File name
    private $dir;       //File directory
    private $postfix;   //Postfix for new file name
    private $fullname;  //Image full name
    public function __construct($fullname){
        $slash=strrpos($fullname, "/");
        $dot=strrpos($fullname, ".");
        $this->dir=substr($fullname, 0, $slash+1);
        $this->name=substr($fullname, $slash+1, $dot-$slash-1);
        $this->type=strtolower(substr($fullname, $dot+1));
        $this->fullname=$fullname;	
    }
    //Minimize jpg
    private function minijpg(){
	$src_img=@imagecreatefromjpeg($this->dir.$this->name.".jpg");
	if(!$src_img) return false;
	$dst_img=ImageCreateTrueColor($this->width,$this->height);
	imagecopyresampled($dst_img,$src_img,0,0,$this->x1,$this->y1,$this->width,$this->height,$this->x2-$this->x1,$this->y2-$this->y1);
	$ret=imagejpeg($dst_img,$this->dir.$this->name.$this->postfix.".jpg", 100);
	imagedestroy($dst_img);
	imagedestroy($src_img);
	return $ret;
    }
    //Minimize gif
    private function minigif(){
	$src_img=@imagecreatefromgif($this->dir.$this->name.".gif");
	if(!$src_img) return false;
	$dst_img=ImageCreateTrueColor($this->width,$this->height);
	imagecopyresampled($dst_img,$src_img,0,0,$this->x1,$this->y1,$this->width,$this->height,$this->x2-$this->x1,$this->y2-$this->y1);
	$ret=imagegif($dst_img,$this->dir.$this->name.$this->postfix.".gif");
	imagedestroy($dst_img);
	imagedestroy($src_img);
	return $ret;
    }
    //Minimize png
    private function minipng(){
	$src_img=@imagecreatefrompng($this->dir.$this->name.".png");
	if(!$src_img) return false;
	$dst_img=ImageCreateTrueColor($this->width,$this->height);
	imagecopyresampled($dst_img,$src_img,0,0,$this->x1,$this->y1,$this->width,$this->height,$this->x2-$this->x1,$this->y2-$this->y1);
	$ret=imagepng($dst_img,$this->dir.$this->name.$this->postfix.".png");
	imagedestroy($dst_img);
	imagedestroy($src_img);
	return $ret;
    }
    private function minibmp(){
	$src_img=@imagecreatefrombmp($this->dir.$this->name.".bmp");
	if(!$src_img) return false;
	$dst_img=ImageCreateTrueColor($this->width,$this->height);
	imagecopyresampled($dst_img,$src_img,0,0,$this->x1,$this->y1,$this->width,$this->height,$this->x2-$this->x1,$this->y2-$this->y1);
	$ret=imagebmp($dst_img,$this->dir.$this->name.$this->postfix.".bmp");
	imagedestroy($dst_img);
	imagedestroy($src_img);
	return $ret;
    }
    //Create square image
    public function resize_square($postfix, $size, $center){
        if($this->type!="jpg"&&$this->type!="gif"&&$this->type!="png"&&$this->type!="bmp"){
            return "";
        }
        $this->width=$size;
        $this->height=$size;
        $this->postfix=$postfix;
	list($w,$h,$t) = getimagesize($this->fullname);
	if($w>=$h){
		if($center){ $this->y1=0; $this->y2=$h; $this->x1=($w-$h)/2; $this->x2=$w-($w-$h)/2; }
		else{ $this->x1=$y1=0; $this->x2=$h; $this->y2=$h; }
	}
	else{
		if($center){ $this->y1=($h-$w)/2; $this->x1=0; $this->y2=$w+($h-$w)/2; $this->x2=$w; }
		else{ $this->x1=$y1=0; $this->x2=$w; $this->y2=$w; }
	}
	if($this->type=="jpg") $ret=$this->minijpg();
	elseif($this->type=="gif") $ret=$this->minigif();
	elseif($this->type=="png") $ret=$this->minipng();
        elseif($this->type=="bmp") $ret=$this->minibmp();
	else $ret=false;
	if($ret) return $this->dir.$this->name.$this->postfix.".".$this->type;
	return "";
    }
    //Create rectangle image
    public function resize_fixed_width($postfix, $sizex){
	if($this->type!="jpg"&&$this->type!="gif"&&$this->type!="png"&&$this->type!="bmp"){
            return "";
        }
	list($w,$h,$t) = getimagesize($this->fullname);
	if($w>$sizex){
	    $sizey=$sizex*$h/$w;
            $this->postfix=$postfix;
            $this->width=$sizex;
            $this->height=$sizey;
            $this->x1=0;
            $this->y1=0;
            $this->x2=$w;
            $this->y2=$h;
	    if($this->type=="jpg") $ret=$this->minijpg();
	    elseif($this->type=="gif") $ret=$this->minigif();
	    elseif($this->type=="png") $ret=$this->minipng();
            elseif($this->type=="bmp") $ret=$this->minibmp();
	    else $ret=false;
	    if($ret) return $this->dir.$this->name.$this->postfix.".".$this->type;
	    return "";
	}
        if($this->postfix){
	    copy($this->fullname, $this->dir.$this->name.$this->postfix.".".$this->type);
	    return $this->dir.$this->name.$this->postfix.".".$this->type;
	}
	return $this->fullname;
    }
    //Create image with old proportions and use max midth or height
    public function resize_max($postfix, $max){
	if($this->type!="jpg"&&$this->type!="gif"&&$this->type!="png"&&$this->type!="bmp"){
            return "";
        }
	list($w,$h,$t) = getimagesize($this->fullname);
	$this->postfix=$postfix;
	$this->x1=0;
	$this->y1=0;
	$this->x2=$w;
	$this->y2=$h;
	if($w>$h){
            $this->width=$max;
            $this->height=$max*$h/$w;
	}
	else{
	    $this->width=$max*$w/$h;
	    $this->height=$max;
	}
	if($this->type=="jpg") $ret=$this->minijpg();
	elseif($this->type=="gif") $ret=$this->minigif();
	elseif($this->type=="png") $ret=$this->minipng();
	elseif($this->type=="bmp") $ret=$this->minibmp();
	else $ret=false;
	if($ret) return $this->dir.$this->name.$this->postfix.".".$this->type;
	return "";
    }
}
?>